<?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 15/11/19
 * Time: 15:11
 */

namespace app\index\behavior;

// use app\index\service\AliBaichuanService;
use app\index\service\JuheSmsService;

/**
 * 发送验证码行为
 * Class MessageSend
 * @package Api\Behavior
 */
class MessageSendBehavior {

    public function run(&$params){

        addLog("SendMessageBehavior",$params,"","[调试]调用发送短消息");

        //如果开启了阿里百川消息服务
        // $service     = new AliBaichuanService();
        // $mobile      = $params['mobile'];
        // $code        = $params['code'];
        // $domain      =  $params['domain'];
        // $domain_text =  $params['domain_text']; //文字描述
        // $context = array(
        //     'appname'      =>'森森水族',
        //     'desc'         =>$domain_text,
        //     'code'         =>$code,
        //     'domain'       =>$domain,
        //     'signature_id' =>'1190',
        //     'template_id'  =>'1758',

        //     'appkey'    =>'23286516',
        //     'secretKey' =>'77805d2a326a04ff8964265709344406',
        // );

        //聚合短信
        $service = new JuheSmsService();
        $mobile      = $params['mobile'];
        $code        = $params['code'];
        $domain      =  $params['domain'];
        $domain_text =  $params['domain_text']; //文字描述
        $context = [
            'desc'   =>$domain_text,
            'code'   =>$code,
            'domain' =>$domain,
            'tpl_id' =>'19213',
            'key'=>'262f0aa5308c4cb7cacd7e62bb906f9e',
        ];

        $result = $service->sendMessage($mobile,$context);
        $params = $result;
        addLog("SendMessageBehavior",$params,$result,"[调试]调用发送短消息返回结果");

    }



    //开发环境下关闭短信发送
//        $appkey = C('JUHE_API.MSG_APPKEY'); #通过聚合申请到数据的appkey
//        $tpl_id = C('JUHE_API.MSG_TPL_ID');
//
//        $sms_config = array(
//            'type'=>SMSServices::JUHE_SMS,//聚合短信
//            'appkey' => $appkey, //您申请的APPKEY
//            'mobile' => $mobile, //接受短信的用户手机号码
//            'tpl_id' => $tpl_id, //您申请的短信模板ID，根据实际情况修改
//            'content' => '#code#=' . $code //您设置的模板变量，根据实际情况修改
//        );
//            $result = SMSServices::send($sms_config);
//
//            $error_code = "-1";
//            $msg = "";
//            if ($result) {
//                $json_result = json_decode($result, true);
//                $error_code = $json_result['error_code'];
//                $msg = $json_result['reason'];
//
//                if (intval($error_code) == 0) {
//                    //状态为0，说明短信发送成功
//                    $this->apiReturnSuc("短信发送成功,短信ID：" . $json_result['result']['sid']);
//                }
//
//            }
//
//            $this->apiReturnErr("短信发送失败(" . $error_code . ")：" . $msg);


}